//
//  HYMyScheduleRoomCell.m
//  Booking
//
//  Created by Duntech on 2018/4/20.
//  Copyright © 2018年 Duntech. All rights reserved.
//

#import "HYMyScheduleRoomCell.h"
#import "HYMyScheduleMeetingRoomModel.h"

@interface HYMyScheduleRoomCell ()

@property (nonatomic,strong) UILabel *statusLabel;
@property (nonatomic,strong) UILabel *floorLabel;
@property (nonatomic,strong) UILabel *roomLabel;
@property (nonatomic,strong) UILabel *meetingNamelabel;
@property (nonatomic,strong) UILabel *bookingTimeLabel;
@property (nonatomic,strong) UIView *line;
@property (nonatomic,strong) UIButton *lookDetailBtn;

@end

@implementation HYMyScheduleRoomCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupSubviews];
        [self setupMasonryLayout];
    }
    return self;
}

- (void)setupSubviews{
    
    [self.contentView addSubview:self.statusLabel];
    [self.contentView addSubview:self.bookingTimeLabel];
    [self.contentView addSubview:self.floorLabel];
    [self.contentView addSubview:self.roomLabel];
    [self.contentView addSubview:self.meetingNamelabel];
    [self.contentView addSubview:self.line];
    [self.contentView addSubview:self.lookDetailBtn];
}

- (void)setupMasonryLayout{
    
    
    CGFloat labelWidth = [self.statusLabel.text widthForFont:KFitFont(15)];
    [_statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self).offset(10 * WIDTH_MULTIPLE);
        make.top.equalTo(self).offset(10 * WIDTH_MULTIPLE);
        make.size.mas_equalTo(CGSizeMake(labelWidth + 10, 20 * WIDTH_MULTIPLE));
    }];
    
    [_bookingTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(self.statusLabel);
        make.left.equalTo(self.statusLabel.mas_right).offset(20 * WIDTH_MULTIPLE);
        make.right.equalTo(self).offset(-20 * WIDTH_MULTIPLE);
    }];
    
    [_roomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.statusLabel);
        make.top.equalTo(self.statusLabel.mas_bottom).offset(10 * WIDTH_MULTIPLE);
        make.width.mas_offset(200 * WIDTH_MULTIPLE);
    }];
    
    [_floorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.statusLabel);
        make.top.equalTo(self.roomLabel.mas_bottom).mas_offset(10 * WIDTH_MULTIPLE);
        make.right.equalTo(self.bookingTimeLabel);
    }];
    
    [_lookDetailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.right.mas_equalTo(self.contentView.mas_right).mas_offset(-10);
        make.size.mas_equalTo(CGSizeMake(68, 34));
    }];
    
    [_meetingNamelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.floorLabel.mas_bottom).offset(10 * WIDTH_MULTIPLE);
        make.left.mas_equalTo(self.statusLabel);
        make.right.mas_equalTo(self.lookDetailBtn.mas_left);
    }];
    
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.equalTo(self.contentView);
        make.height.mas_equalTo(1);
    }];
}

- (void)configWithModel:(id)model{
    
    HYMyScheduleMeetingRoomModel *cellModel = (HYMyScheduleMeetingRoomModel *)model;
    // start_time = "2018-04-26T12:30:00",
    NSString *startTime = [cellModel.startTime stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    NSString *endTime = [cellModel.endTime stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    NSDate *startDate = [NSDate dateWithString:startTime format:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *endDate = [NSDate dateWithString:endTime format:@"yyyy-MM-dd HH:mm:ss"];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    NSString *startTimeStr = [dateFormatter stringFromDate:startDate];
    NSString *endTimeStr = [dateFormatter stringFromDate:endDate];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateStr = [dateFormatter stringFromDate:startDate];
    
    switch ([cellModel.signStatus integerValue]) {
        case 1:
            _statusLabel.text = NSLocalizedString(@"confirmStatus", nil);
            _statusLabel.backgroundColor = KAPP_Available_COLOR;
            break;
        case 2:
            _statusLabel.text = NSLocalizedString(@"currentStatus", nil);
            _statusLabel.backgroundColor = RGBColor(52, 191, 206);
            break;
        case 3:
            _statusLabel.text = NSLocalizedString(@"expiredStatus", nil);
            _statusLabel.backgroundColor = RGBColor(181, 181, 181);
            break;
            
        default:
            _statusLabel.text = @"未知";
            _statusLabel.backgroundColor = RGBColor(181, 181, 181);
            break;
    }

    _bookingTimeLabel.text = [NSString stringWithFormat:@"%@  %@-%@",dateStr,startTimeStr,endTimeStr];
    _roomLabel.text = cellModel.meetingroomName;
    _floorLabel.text = [NSString stringWithFormat:@"%@  %@  %@",cellModel.cityName,cellModel.buildName,cellModel.floorName];
    _meetingNamelabel.text = [NSString stringWithFormat:@"%@：%@",NSLocalizedString(@"meetingName", nil),cellModel.markName];
    
}

-(void)lookDeatilAction{
    if (self.lookDetailBlock) {
        self.lookDetailBlock();
    }
}

#pragma mark - lazyload
- (UILabel *)statusLabel{
    
    if (!_statusLabel) {
        _statusLabel = [UILabel new];
        _statusLabel.textAlignment = NSTextAlignmentCenter;
        _statusLabel.font = KFitBoldFont(15);
        _statusLabel.textColor = KAPP_WHITE_COLOR;
        _statusLabel.text = NSLocalizedString(@"confirmStatus", nil);
        _statusLabel.layerCornerRadius = 3;
    }
    return _statusLabel;
}

- (UILabel *)meetingNamelabel{
    
    if (!_meetingNamelabel) {
        _meetingNamelabel = [UILabel new];
        _meetingNamelabel.textAlignment = NSTextAlignmentLeft;
        _meetingNamelabel.font = KFitFont(15);
        _meetingNamelabel.textColor = [UIColor grayColor];
    }
    return _meetingNamelabel;
}

- (UILabel *)bookingTimeLabel{
    
    if (!_bookingTimeLabel) {
        _bookingTimeLabel = [UILabel new];
        _bookingTimeLabel.textAlignment = NSTextAlignmentLeft;
        _bookingTimeLabel.font = KFitFont(13);
        _bookingTimeLabel.textColor = [UIColor grayColor];
    }
    return _bookingTimeLabel;
}

- (UILabel *)floorLabel{
    
    if (!_floorLabel) {
        _floorLabel = [UILabel new];
        _floorLabel.textAlignment = NSTextAlignmentLeft;
        _floorLabel.font = KFitFont(14);
        _floorLabel.textColor = KAPP_BLACK_COLOR;
    }
    return _floorLabel;
}


- (UILabel *)roomLabel{
    
    if (!_roomLabel) {
        _roomLabel = [UILabel new];
        _roomLabel.textAlignment = NSTextAlignmentLeft;
        _roomLabel.font = KFitFont(16);
        _roomLabel.textColor = KAPP_BLACK_COLOR;
    }
    return _roomLabel;
}

- (UIView *)line{
    
    if (!_line) {
        _line = [UIView new];
        _line.backgroundColor = KAPP_SEPERATOR_COLOR;
    }
    return _line;
}

-(UIButton *)lookDetailBtn{
    if (!_lookDetailBtn) {
        _lookDetailBtn = [[UIButton alloc]init];
        [_lookDetailBtn setTitle:NSLocalizedString(@"view", nil) forState:(UIControlStateNormal)];
        _lookDetailBtn.titleLabel.font = KFitFont(18);
        [_lookDetailBtn setTitleColor:RGBColor(236, 63, 149) forState:(UIControlStateNormal)];
        _lookDetailBtn.layerCornerRadius = 5.0;
        _lookDetailBtn.layerBorderWidth = 1.0;
        _lookDetailBtn.layerBorderColor = RGBColor(236, 63, 149);
        [_lookDetailBtn addTarget:self action:@selector(lookDeatilAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _lookDetailBtn;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
